package iagovsothello.online.server;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Record implements Comparable<Record>, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -700727955273188243L;
	private String ID;
	private String name;
	private String surname;
	private LinkedList<Game> games;
	private int gamesLeft;
	private int points;
	private int totalDisks;
	
	public Record(String ID, String name, String surname) 
	{
		super();
		this.ID = ID;
		this.name = name;
		this.surname = surname;
		this.games = new LinkedList<Game>();
		this.gamesLeft = 3;
		this.points = 0;
		this.totalDisks = 0;
	}

	public void computePoints()
	{
		points = 0;
		totalDisks = 0;
		for (Game game : games)
		{
			if (game.isValid())
			{
				if (game.isHumanVictorious())
				{
					points += difficultyPoints(game.getDifficulty())*outcomePoints(1);
				}
				else if (game.draw())
				{
					points += difficultyPoints(game.getDifficulty())*outcomePoints(0);
				}
				if (game.getDifficulty() >= 3)
					totalDisks += game.getHumanPoints()-game.getAiPoints();
			}
		}
	}
	
	public void addGame(Game game)
	{
		games.add(game);
	}
	
	public Iterator<Game> gameIterator()
	{
		return games.iterator();
	}
	
	public List<Game> getGames()
	{
		return games;
	}
	
	public Game getGame(String code)
	{
		Iterator<Game> it = games.iterator();
		boolean found = false;
		Game game = null;
		while(!found && it.hasNext())
		{
			game = it.next();
			found = game.getCode().equals(code);
		}
		return game;
	}
	
	public String getID() 
	{
		return ID;
	}

	public void setID(String iD) 
	{
		ID = iD;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public String getSurname() 
	{
		return surname;
	}

	public void setSurname(String surname) 
	{
		this.surname = surname;
	}
	
	public int getGamesLeft() 
	{
		return gamesLeft;
	}

	public void setGamesLeft(int gamesLeft) 
	{
		this.gamesLeft = gamesLeft;
	}
	
	public int getPoints() 
	{
		return points;
	}

	public void setPoints(int points) 
	{
		this.points = points;
	}
	
	public int getTotalDisks() 
	{
		return totalDisks;
	}

	public void setTotalDisks(int totalDisks) 
	{
		this.totalDisks = totalDisks;
	}

	@Override
	public int compareTo(Record o) 
	{
		return ID.compareTo(o.ID);
	}

	private int outcomePoints(int result)
	{
		if (result > 0)
			return 2;
		else if (result == 0)
			return 1;
		else
			return 0;
	}
	
	private int difficultyPoints(int difficulty)
	{
		switch (difficulty)
		{
		case 3:
			return 1;
		case 4:
			return 2;
		case 5: 
			return 7;
		default:
			return 0;
		}
	}
}
